package com.ican.oc.service;


import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ican.common.SystemConstants;
import com.ican.oc.dao.OcOptionDao;
import com.ican.oc.dao.OcOptionDescriptionDao;
import com.ican.oc.dao.OcOptionValueDao;
import com.ican.oc.dao.OcOptionValueDescriptionDao;
import com.ican.oc.domain.OcOption;
import com.ican.oc.domain.OcOptionDescPK;
import com.ican.oc.domain.OcOptionDescription;
import com.ican.oc.domain.OcOptionValue;
import com.ican.oc.domain.OcOptionValueDescPK;
import com.ican.oc.domain.OcOptionValueDescription;

@Service("ocOptionAndValueService")
public class OcOptionAndValueService{

    private OcOptionDao ocOptionDao;
    private OcOptionDescriptionDao ocOptionDescriptionDao;
    private OcOptionValueDao ocOptionValueDao;
    private OcOptionValueDescriptionDao ocOptionValueDescriptionDao;
    
    public OcOptionDescription addOptionAndDesc(String nameCode, String name, Integer langId){
        OcOptionDescription opDesc = ocOptionDescriptionDao.findOptionDescByNameCodeAndLang(nameCode, SystemConstants.LANG_ID_CN);
        if(opDesc == null){//没有属性
            OcOption option = new OcOption();
            option.setSortOrder(0);
            option.setType(SystemConstants.NODE_TYPE_SELECT);
            ocOptionDao.save(option);
            
            opDesc = new OcOptionDescription();
            opDesc.setPk(new OcOptionDescPK());
            opDesc.getPk().setLanguageId(SystemConstants.LANG_ID_CN);
            opDesc.getPk().setOptionId(option.getOptionId());
            opDesc.setName(nameCode);
            ocOptionDescriptionDao.save(opDesc);
            
            if(langId != SystemConstants.LANG_ID_CN){
                OcOptionDescription temp = new OcOptionDescription();
                temp.setPk(new OcOptionDescPK());
                temp.getPk().setLanguageId(langId);
                temp.getPk().setOptionId(option.getOptionId());
                temp.setName(name);
                ocOptionDescriptionDao.save(temp);
            }
        }else{//有中文名称的属性了
            if(langId != SystemConstants.LANG_ID_CN){
                OcOptionDescription temp = ocOptionDescriptionDao.findOptionDescByNameAndLang(opDesc.getPk().getOptionId(), name, langId);
                if(temp == null){
                    temp = new OcOptionDescription();
                    temp.setPk(new OcOptionDescPK());
                    temp.getPk().setLanguageId(langId);
                    temp.getPk().setOptionId(opDesc.getPk().getOptionId());
                    temp.setName(name);
                    ocOptionDescriptionDao.save(temp);
                }
            }
        }
        return opDesc;
    }
    
    public OcOptionValueDescription addOptionValueAndDesc(Integer optionId, String valueCode, String value, Integer langId){
        OcOptionValueDescription opValDesc = ocOptionValueDescriptionDao.findOptionValueDescByValueCodeAndLang(optionId, valueCode, SystemConstants.LANG_ID_CN);
        if(opValDesc == null){//没有属性值
            OcOptionValue optionValue = new OcOptionValue();
            optionValue.setOptionId(optionId);
            optionValue.setSortOrder(0);
            optionValue.setImage("");
            ocOptionValueDao.save(optionValue);
            
            opValDesc = new OcOptionValueDescription();
            opValDesc.setPk(new OcOptionValueDescPK());
            opValDesc.getPk().setLanguageId(SystemConstants.LANG_ID_CN);
            opValDesc.getPk().setOptionValueId(optionValue.getOptionValueId());
            opValDesc.setOptionId(optionId);
            opValDesc.setName(valueCode);
            ocOptionValueDescriptionDao.save(opValDesc);
            
            if(langId != SystemConstants.LANG_ID_CN){
                OcOptionValueDescription temp = new OcOptionValueDescription();
                temp.setPk(new OcOptionValueDescPK());
                temp.getPk().setLanguageId(langId);
                temp.getPk().setOptionValueId(optionValue.getOptionValueId());
                temp.setOptionId(optionId);
                temp.setName(value);
                ocOptionValueDescriptionDao.save(temp);
            }
        }else{//有中文名称的属性值了
            if(langId != SystemConstants.LANG_ID_CN){
                OcOptionValueDescription temp = ocOptionValueDescriptionDao.findOptionValueDescByValueAndLang(opValDesc.getPk().getOptionValueId(), value, langId);
                if(temp == null){
                    temp = new OcOptionValueDescription();
                    temp.setPk(new OcOptionValueDescPK());
                    temp.getPk().setLanguageId(langId);
                    temp.getPk().setOptionValueId(opValDesc.getPk().getOptionValueId());
                    temp.setOptionId(optionId);
                    temp.setName(value);
                    ocOptionValueDescriptionDao.save(temp);
                }
            }
        }
        return opValDesc;
    }

    @Resource
    public void setOcOptionDao(OcOptionDao ocOptionDao) {
        this.ocOptionDao = ocOptionDao;
    }
    @Resource
    public void setOcOptionDescriptionDao(OcOptionDescriptionDao ocOptionDescriptionDao) {
        this.ocOptionDescriptionDao = ocOptionDescriptionDao;
    }
    @Resource
    public void setOcOptionValueDao(OcOptionValueDao ocOptionValueDao) {
        this.ocOptionValueDao = ocOptionValueDao;
    }
    @Resource
    public void setOcOptionValueDescriptionDao(OcOptionValueDescriptionDao ocOptionValueDescriptionDao) {
        this.ocOptionValueDescriptionDao = ocOptionValueDescriptionDao;
    }
}
